;(function($){
	$.fn.spa_change_table = function(opts){
		var el = this;
		var opt = {
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initChangeReason();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//选择备注
    		el.find(".orderTaste").on("click","button",function(){
    			remark = el.find(".modal-body textarea").val();
    			if($(this).hasClass("btn-primary")){
    				$(this).removeClass("btn-primary").addClass("btn-light");
    				remark = remark.replace(""+ $(this).text() +",","");
    			}else{
    				$(this).removeClass("btn-light").addClass("btn-primary");
					remark = remark + $(this).text() + ",";    						
    			}
    			el.find("#remark").val(remark);
    		});

			//返回
			el.find("#modalBtnCancel,#close-dialog").unbind("click").click(function(){
				el.modal("hide")
				param.closeBack();
				el.empty();
			});
			
			//查询台号
			el.find("#btnModalSearchTabel").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){  
					//获取菜品列表
					if(!$.isFunction($().spa_search_table)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_search_table.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).modal("show"); 
					el.modal("toggle");
					$("#"+modalEle).spa_search_table({
						th : el.find("#pay").val(),
						closeBack:function(){
							el.modal("toggle");
						},
						dataBack:function(th){
							el.modal("toggle");
							el.find("#to_th").val(th);
						},
					});
				});
			});
			
			//确定换台
			el.find("#modalBtnSave").unbind("click").click(function(){
    			var postData={"arg":{no:param.no,to_th:$("#to_th").val(),reason:el.find("#remark").val()}};
				var output={}
				t.saveChangeTable(postData,output)
			});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp(param.title)+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
								
									'<form class="form-horizontal" role="form">'+
									
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("台号")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:245px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="to_th" id="to_th" value="">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="btnModalSearchTabel">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("备注:")+'</label>'+
											'<div class="col-sm-9">'+
												'<span class="input-icon input-icon-right">'+
													'<textarea class="form-control search-query" cols="50" rows="5" placeholder="" name="disrate" id="remark"></textarea>'+
												'</span>'+
											'</div>'+
										'</div>'+
										
									'</form>'+
									
									'<div class="widget-box modal-list transparent" style="max-height:120px;">'+  
										'<div class="orderTaste"></div>'+
									'</div>'+
									
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("确定")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-undo"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		initChangeReason : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var data = sysgetdict("换台原因");
			t.reasonData(data);
		},
		//绑定事件
    	reasonData:function(data){
			
			var el = this.el;
			var param = this.param;
			var t = this;
    		debugger
    		if(data.length > 0 && JSON.stringify(data)!="[{}]"){
    			if(Math.ceil(data.length / 4) > 5){
    				el.find(".modal-list .orderTaste").css({"width":499});
    			}else{
    				el.find(".modal-list .orderTaste").css({"width":549});
    			}
				$(data).each(function(key,value){
					var btn_html = $('<button type="button" class="btn btn-app btn-light">'+ value.descript1 +'</button>');
    				if(value.descript1.length > Math.floor(((el.find(".orderTaste").width() - 18) / 4) / 18)){
    					btn_html.css("font-size",((el.find(".orderTaste").width() - 18) / 4) / value.descript1.length);
        			}
    				el.find(".modal-list .orderTaste").append(btn_html);
	    		});
				el.find(".modal-list button").css({"width":(el.find(".orderTaste").width() - 18) / 4,"height":49.3});
				el.find(".modal-list .orderTaste").css("height",Math.ceil(data.length / 4)*50);
    		}
    	},
		saveChangeTable : function(postData,output){
    		var t = this;
			var el = this.el;
			var param = this.param;
    		
    		t.wait();
			$.ajax({
    			url: $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
    			type: "post",
    			dataType: "json",
    			data: $.HSAPI_ASKJSONINFO(JSON.stringify(postData),'POSOrderTHChange',JSON.stringify(output)),
				success : function(data){
					t.goon();
					if(data.RETURN==1){
						jf_success("换台成功!")
						el.modal("hide");
						el.empty();
						param.dataBack(postData.arg.to_th)
					}else if(data.RETURN==-10){
						if(!$.isFunction($().alertModal)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.alertModal.js");
				            $.ajaxSetup({ async: true });
						}
						$("").alertModal({
							data:data,
							getDefaultBack:function(json){
								if(!postData.arg){
									postData.arg = {}
								}
								for(k in json){
									postData.arg[k] = json[k];
								}
								t.saveChangeTable(postData,output);
							}
						})
					}else{
						jf_alert(data.MSG);
					}
				},
				error : function(data){
					t.goon();
					jf_alter(data)
				},
    		});
    	},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
	
})(jQuery);